package com.ldj.hibernate;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;

import com.ldj.struts.form.SuperviseOrganForm;

public class SuperviseOrganDAO extends BaseHibernateDAO {
	public SuperviseOrganDAO(HttpServletRequest request, HttpServletResponse response) {
		super(request, response);
		// TODO Auto-generated constructor stub
	}

	private static final Log log = LogFactory.getLog(SuperviseOrganDAO.class);
	
	//查找所有的Reporter 加入分页功能
	public List findAllPage( ) {
		log.debug("finding all SuperviseOrganDAO instances");
		try {
			String queryString = "from SuperviseOrgan ";
			Query queryObject = getSession().createQuery(queryString);
			this.setPageSize(8);
			return this.jlist(queryObject);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	
	}
	
	public SuperviseOrgan findById(java.lang.String id) {
		log.debug("getting SuperviseOrgan instance with id: " + id);
		try {
			SuperviseOrgan instance = (SuperviseOrgan) getSession().get(
					"com.ldj.hibernate.SuperviseOrgan", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
//	 //管理范围
//	 public List findManageScopeByUserId(String pid) {
//			log.debug("getting UserManagerScope instance with pid: " + pid);
//			try {
//				String queryString = "from UserManagerScope where userManagerScopeUserId = '" + pid + "'";
//				Query queryObject = getSession().createQuery(queryString);
//				return queryObject.list();
//			} catch (RuntimeException re) {
//				log.error("find all failed", re);
//				throw re;
//			}
//		}
//	 
//	 public List getLogin(String code, String value)
//	    {
//	        log.debug("finding all RolePowerInfo instances");
//	        try
//	        {
//	        	String queryString = "from DataDictionary where dataDictionaryCode like '" + code + "___' and dataDictionaryCode in('"+value+"')";
//	            Query queryObject = getSession().createQuery(queryString);
//	            return queryObject.list();
//	        }
//	        catch(RuntimeException re)
//	        {
//	            log.error("find all failed", re);
//	            throw re;
//	        }
//	    }
//	 
	
	public void save(SuperviseOrgan transientInstance) {
		log.debug("saving SuperviseOrgan instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	public SuperviseOrgan merge(SuperviseOrgan detachedInstance) {
		try {
			SuperviseOrgan result = (SuperviseOrgan) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}
	
	public void delete(SuperviseOrgan persistentInstance) {
		log.debug("deleting SuperviseOrgan instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	
	public List getSonOrganFromCode(String code)
    {
        log.debug("finding all Catagory instances");
        try
        {
        	String queryString = "from SuperviseOrgan where supOrganDelOper='0' and supOrganCode like '"+code+"___' order by supOrganCode ";
            Query queryObject = getSession().createQuery(queryString);
            return queryObject.list();
        }
        catch(RuntimeException re)
        {
            log.error("find all failed", re);
            throw re;
        }
    }
	
	 public List getSonCatalog(String code)
	    {
	        log.debug("finding all Catagory instances");
	        try
	        {
	        	String queryString = "from SuperviseOrgan where supOrganDelOper='0' and supOrganCode like '"+code+"%' order by supOrganCode ";
	            Query queryObject = getSession().createQuery(queryString);
	            return queryObject.list();
	        }
	        catch(RuntimeException re)
	        {
	            log.error("find all failed", re);
	            throw re;
	        }
	    }
	 
	 public List getSonOrgan(String value)
	    {
	        log.debug("finding all Catagory instances");
	        try
	        {
	        	String queryString = "from SuperviseOrgan where supOrganDelOper='0' and supOrganCode in('"+ value +"') order by supOrganCode ";
	            Query queryObject = getSession().createQuery(queryString);
	            return queryObject.list();
	        }
	        catch(RuntimeException re)
	        {
	            log.error("find all failed", re);
	            throw re;
	        }
	    }
	 
	 public List findAllPage(String code,SuperviseOrganForm sForm) {
			log.debug("finding all Kind instances");
			try {
				String queryString =(new StringBuilder("from SuperviseOrgan where supOrganDelOper='0' and supOrganCode like '")).append(code).append("___'").toString();
				if(sForm.getSupOrganName() != null && !"".equals(sForm.getSupOrganName())){
					queryString += " and supOrganName like '%" + sForm.getSupOrganName() + "%'";
				}
				if(sForm.getSupOrganAdministrativeLevel() != null && !"".equals(sForm.getSupOrganAdministrativeLevel())){
					queryString += " and supOrganAdministrativeLevel like '%" + sForm.getSupOrganAdministrativeLevel()+ "%'";
				}
				queryString +="order by supOrganCode";
				Query queryObject = getSession().createQuery(queryString);
				this.setPageSize(9);
				return this.jlist(queryObject);
			} catch (RuntimeException re) {
				log.error("find all failed", re);
				throw re;
			}
		}
	 
	 public List findByCode(String code) {
			log.debug("getting SuperviseOrgan instance with code: " + code);
			try {
				String queryString = "from SuperviseOrgan where supOrganCode like '" + code + "%'";
				Query queryObject = getSession().createQuery(queryString);
				return queryObject.list();
			} catch (RuntimeException re) {
				log.error("find all failed", re);
				throw re;
			}
		}
	 public List findBySupOrganCode(String code) {
			log.debug("getting SuperviseOrgan instance with code: " + code);
			try {
				String queryString = "from SuperviseOrgan where supOrganDelOper='0' and supOrganCode = '" + code + "'";
				Query queryObject = getSession().createQuery(queryString);
				return queryObject.list();
			} catch (RuntimeException re) {
				log.error("find all failed", re);
				throw re;
			}
		}
	 
	 public SuperviseOrgan findByAllSupOrganCode(String code) {
			log.debug("getting SuperviseOrgan instance with code: " + code);
			try {
				String queryString = "from SuperviseOrgan where supOrganCode = '" + code + "'";
				Query queryObject = getSession().createQuery(queryString);
				List list = queryObject.list();
				if(list != null && list.size() > 0){
					SuperviseOrgan organ = (SuperviseOrgan)list.get(0);
					return organ;
				}
				return null;
			} catch (RuntimeException re) {
				log.error("find all failed", re);
				throw re;
			}
		}
	 
	 //用户管理范围
	 public List findBySupCode(String uid) {
			log.debug("getting UserManagerScope instance with code: " + uid);
			try {
				String queryString = "from UserManagerScope where userManagerScopeUserId = '" + uid + "'";
				Query queryObject = getSession().createQuery(queryString);
				return queryObject.list();
			} catch (RuntimeException re) {
				log.error("find all failed", re);
				throw re;
			}
		}
	 
	 
	//用户管理范围
	 public UserManagerScope merge(UserManagerScope detachedInstance) {
			log.debug("merging UserManagerScope instance");
			try {
				UserManagerScope result = (UserManagerScope) getSession().merge(
						detachedInstance);
				log.debug("merge successful");
				return result;
			} catch (RuntimeException re) {
				log.error("merge failed", re);
				throw re;
			}
		}
	 
	 public void deleteSuper(UserManagerScope persistentInstance) {
			log.debug("deleting UserManagerScope instance");
			try {
				getSession().delete(persistentInstance);
				log.debug("delete successful");
			} catch (RuntimeException re) {
				log.error("delete failed", re);
				throw re;
			}
		}
	 //保存管理范围
	 public void saveSuper(UserManagerScope transientInstance) {
			log.debug("saving UserManagerScope instance");
			try {
				getSession().save(transientInstance);
				log.debug("save successful");
			} catch (RuntimeException re) {
				log.error("save failed", re);
				throw re;
			}
		}
	 
	 //查找带有权限的树
	 public List getPowerCode(String value)
	    {
	        log.debug("finding all RolePowerInfo instances");
	        try
	        {
	        	String queryString = "from SuperviseOrgan where supOrganDelOper='0' and supOrganCode in('"+value+"') order by supOrganCode";
	            Query queryObject = getSession().createQuery(queryString);
	            return queryObject.list();
	        }
	        catch(RuntimeException re)
	        {
	            log.error("find all failed", re);
	            throw re;
	        }
	    }
}